﻿using System;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;
using Dict.Blo;


namespace UI
{
    public class UITreasureFrag
    {
        public Dictionary<string, UIFragItem> frags = new Dictionary<string, UIFragItem>();
        public OrderDictionary<string, TableTreasure> mayCombineTreas = new OrderDictionary<string, TableTreasure>();

        public void ResetData(List<SimpleItem> data)
        {
            frags.Clear();
            mayCombineTreas.Clear();
            var mustList = DictTreasureBlo.GetMustShowTreasure();
            for(int i = 0; i < mustList.Count;++i)
            {
                mayCombineTreas.Add(mustList[i].id, mustList[i]);
            }
            for (int i = 0; i < data.Count; ++i)
            {
                if(data[i].num <= 0)
                {
                    continue;
                }
                var fi = new UIFragItem(data[i]);
                frags.Add(data[i].id, fi);
                if (!mayCombineTreas.ContainsKey(fi.fragData.target))
                {
                    mayCombineTreas.Add(fi.fragData.target, DictTreasureBlo.GetTableTreasure(fi.fragData.target));
                }
            }
            mayCombineTreas.Sort(Compare);
        }

        public List<UIFragItem> GetNeedFragCell(string treaId)
        {
            List<TableFrag> tfs = DictFragBlo.GetFragByTarget(treaId);
            List<UIFragItem> res = new List<UIFragItem>();
            for(int i = 0; i < tfs.Count;++i)
            {
                if(frags.ContainsKey(tfs[i].id))
                {
                    res.Add(frags[tfs[i].id]);
                }
                else
                {
                    res.Add(new UIFragItem(tfs[i].id));
                }
            }
            return res;
        }

        public void Recaculate()
        {
            mayCombineTreas.Clear();

            foreach(var item in frags)
            {
                if (!mayCombineTreas.ContainsKey(item.Value.fragData.target))
                {
                    mayCombineTreas.Add(item.Value.fragData.target, DictTreasureBlo.GetTableTreasure(item.Value.fragData.target));
                }
            }
            mayCombineTreas.Sort(Compare);
        }

        public bool CouldCommineTarget(string treaId)
        {
            List<TableFrag> tfs = DictFragBlo.GetFragByTarget(treaId);
            //List<UIFragItem> res = new List<UIFragItem>();
            for (int i = 0; i < tfs.Count; ++i)
            {
                if (!frags.ContainsKey(tfs[i].id) || frags[tfs[i].id].num < tfs[i].num)
                {
                    return false;
                }
            }
            return true;
        }

        public bool HasFrag(string treaId)
        {
            List<TableFrag> tfs = DictFragBlo.GetFragByTarget(treaId);
            for (int i = 0; i < tfs.Count; ++i)
            {
                if (frags.ContainsKey(tfs[i].id) && frags[tfs[i].id].num > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public void RemoveMayItem(string treaId)
        {
            mayCombineTreas.Remove(treaId);
        }

        public int GetFragNumber(string fragId)
        {
            if(frags.ContainsKey(fragId))
            {
                return (int)frags[fragId].num;
            }
            else
            {
                return 0;
            }
        }

        public void CostFrag(BaseResource br)
        {
            if(frags.ContainsKey(br.id))
            {
                frags[br.id].num -= br.num;
                if(frags[br.id].num <= 0)
                {
                    frags.Remove(br.id);
                }
            }
        }

        public int Compare(TableTreasure a, TableTreasure b)
        {
            return b.quality - a.quality;
        }

        public void DeleteFrag(SimpleItem si)
        {
            if (frags.ContainsKey(si.id))
            {
                frags[si.id].num -= si.num;
                if (frags[si.id].num <= 0)
                {
                    frags.Remove(si.id);
                }
            }
        }

        public UIFragItem GetFrag(string id)
        {
            if(frags.ContainsKey(id))
            {
                return frags[id];
            }
            else
            {
                return new UIFragItem(id);
            }
        }

        public void SetFrag(SimpleItem si)
        {
            if (frags.ContainsKey(si.id))
            {
                frags[si.id].num = si.num;
            }
            else
            {
                frags.Add(si.id, new UIFragItem(si));
            }
        }

        public void AddFrag(SimpleItem si)
        {
            if(frags.ContainsKey(si.id))
            {
                frags[si.id].num += si.num;
            }
            else
            {
                frags.Add(si.id,new UIFragItem(si));
            }
        }

        public void Clear()
        {
            frags.Clear();
            mayCombineTreas.Clear();
        }
    }
}
